package com.hj.system.system.controller;

import com.hj.system.base.annotation.SystemLog;
import com.hj.system.base.config.Constant;
import com.hj.system.common.controller.BaseController;
import com.hj.system.base.domain.Tree;
import com.hj.system.base.utils.ResponseUtil;
import com.hj.system.system.domain.MenuDO;
import com.hj.system.system.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wxf
 */
@RequestMapping("/sys/menu")
@Controller
public class MenuController extends BaseController {
    String prefix = "system/menu";
    @Autowired
    MenuService menuService;

    @GetMapping()
    public String menu() {
        return prefix + "/menu";
    }

    @RequiresPermissions("sys:menu:menu")
    @RequestMapping("/list")
    @ResponseBody
    public List<MenuDO> list(@RequestParam Map<String, Object> params) {
        List<MenuDO> menus = menuService.list(params);
        return menus;
    }

    @SystemLog("添加菜单")
    @RequiresPermissions("sys:menu:add")
    @GetMapping("/add/{pId}")
    public String add(Model model, @PathVariable("pId") String pId) {
        handleAddEdit(pId, model);
        return prefix + "/add";
    }

    private void handleAddEdit(String pId, Model model) {
        model.addAttribute("pId", pId);
        if ("0".equalsIgnoreCase(pId)) {
            model.addAttribute("pName", Constant.MENU_ROOT_NAME);
        } else {
            model.addAttribute("pName", menuService.get(pId).getName());
        }
    }

    @SystemLog("编辑菜单")
    @RequiresPermissions("sys:menu:edit")
    @GetMapping("/edit/{id}")
    public String edit(Model model, @PathVariable("id") String id) {
        MenuDO mdo = menuService.get(id);
        String pId = mdo.getParentId();
        handleAddEdit(pId, model);
        model.addAttribute("menu", mdo);
        return prefix + "/edit";
    }

    @SystemLog("保存菜单")
    @RequiresPermissions("sys:menu:add")
    @PostMapping("/save")
    @ResponseBody
    public ResponseUtil save(MenuDO menu) {
        if (menuService.save(menu) > 0) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.error(1, "保存失败");
        }
    }

    @SystemLog("更新菜单")
    @RequiresPermissions("sys:menu:edit")
    @PostMapping("/update")
    @ResponseBody
    public ResponseUtil update(MenuDO menu) {
        if (menuService.update(menu) > 0) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.error(1, "更新失败");
        }
    }

    @SystemLog("删除菜单")
    @RequiresPermissions("sys:menu:remove")
    @PostMapping("/remove")
    @ResponseBody
    public ResponseUtil remove(String id) {
        if (menuService.remove(id) > 0) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.error(1, "删除失败");
        }
    }

    /**
     * 根据角色查询菜单权限
     * 角色分配权限使用
     */
    @GetMapping("/tree/{roleId}")
    @ResponseBody
    public Tree<MenuDO> tree(@PathVariable("roleId") String roleId) {
        Tree<MenuDO> tree = menuService.getTree(roleId);
        return tree;
    }
}
